﻿// <copyright file="InfiltratorValidator.cs" company="City of Portland, BES-ASM">
// </copyright>
// <summary>InfiltratorValidator class</summary>

namespace BESASM.EMGAATS.BusinessClasses
{
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using FluentValidation;

  /// <summary>
  /// Validator for the Infiltrator class
  /// </summary>
  public class InfiltratorValidator : AbstractValidator<IInfiltrator>
  {
    /// <summary>
    /// Initializes a new instance of the <see cref="InfiltratorValidator"/> class
    /// </summary>
    /// <param name="ruleSet">The set of rules to apply</param>
    public InfiltratorValidator(RuleSet ruleSet)
    {
      switch (ruleSet)
      {
        case BusinessClasses.RuleSet.MasterDataCommit:
          this.InfiltratorRateGreaterThanZero(ErrorLevel.Error);
          this.InfiltratorNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDataCommit:
          this.InfiltratorRateGreaterThanZero(ErrorLevel.Error);
          this.InfiltratorNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.ModelDeployment:
          this.InfiltratorRateGreaterThanZero(ErrorLevel.Error);
          this.InfiltratorNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.NetworkTracing:
          this.InfiltratorRateGreaterThanZero(ErrorLevel.Error);
          this.InfiltratorNodeExists(ErrorLevel.Error);
          break;
        case BusinessClasses.RuleSet.PropertyDialog:
          this.InfiltratorRateGreaterThanZero(ErrorLevel.Error);
          this.InfiltratorNodeExists(ErrorLevel.Warning);
          break;
      }
    }

    private void InfiltratorRateGreaterThanZero(ErrorLevel level)
    {
      RuleFor(infiltrator => infiltrator.RateInPerHr)
        .GreaterThan(0.0)
        .WithState(infiltrator => new ValidationState(infiltrator, level))
        .WithMessage(
          "Infiltrator {0} at node {1} has a rate equal to or less than zero",
          infiltrator => infiltrator.InfiltratorName,
          infiltrator => infiltrator.Node == null ? "--" : infiltrator.Node.NodeName);
    }

    private void InfiltratorNodeExists(ErrorLevel level)
    {
      RuleFor(infiltrator => infiltrator.Node)
        .NotNull()
        .WithState(infiltrator => new ValidationState(infiltrator, level))
        .WithMessage(
          "Infiltrator {0} is not attached to a node",
          infiltrator => infiltrator.InfiltratorName);
    }
  }
}
